<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use DB;
class CollectController extends Controller
{
	//显示收藏
	public function getIndex(Request $request){
		// dd($request->all());
		//获取全部数据
		$collect = DB::table('shop_collect')
            ->join('shop_users', 'shop_collect.uid', '=', 'shop_users.uid')
            ->join('shop_goods', 'shop_collect.gid', '=', 'shop_goods.gid')
            ->select('shop_collect.*', 'shop_users.*', 'shop_goods.*')
            ->get();
		// dd($collect);
		return view('home.collect.collect',['collect'=>$collect]);
	}
	//添加收藏
	public function postAdd(Request $request){
		// dd($request->all());
		if(session('uid')==null){
			echo '错误';
		}
		$data['gid'] = $request->input('gid');
		$data['uid'] = session('uid');
		//判断是否已添加该商品
		if(DB::table('shop_collect')->where($data)->first()){
			echo '重复添加';
		}else{
			//添加到数据库
			$res = DB::table('shop_collect')->insert($data);
			echo $res;
		}
		
		
	}
	//删除收藏
	public function getDel(Request $request)
    {
    	$data['gid'] = $request->input('gid');
    	$data['uid'] = session('uid');
    	//执行删除
    	$res = DB::table('shop_collect')->where($data)->delete();
    	echo $res;
    }

}
